// stores/user.ts
import { defineStore } from 'pinia'

interface UserState {
    yhm: string,
    id:  string,
    isLoggedIn: boolean  // 新增的登录状态字段
}

// @ts-ignore
// @ts-ignore
export const useUserStore = defineStore('user', {
    state: (): UserState => ({
        yhm: '',
        id: '',
        isLoggedIn: false

    }),
    actions: {
        setUsername(name: string): void {
            this.yhm = name
            this.isLoggedIn = true
        },
        setUserId(id: string): void {
            this.id = id
            this.isLoggedIn = true
        },
        clearUserName(): void {
            this.yhm = ''
            this.isLoggedIn = false
        },
        clearUserId(): void {
            this.id = ''
            this.isLoggedIn = false
        }
    },
    persist: {
        key: 'user',  // 存储的键名
        storage: localStorage,  // 使用 localStorage
        paths: ['yhm', 'id', 'isLoggedIn']  // 指定要持久化的字段
    }
})